<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

const COMMENT = ' 当设置 true 的时候该路由不会在侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1';
class CreateAdminTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admin_users', function (Blueprint $table) {
            $table->id();
            $table->string('username', 64)->comment('后台用户登录账号');
            $table->string('password', 80)->comment('后台用户登录密码');
            $table->string('name', 64)->comment('后台用户昵称');
            $table->string('avatar',191)->nullable()->default('')->comment('后台用户头像');
            $table->string('remember_token', 100)->nullable()->default('')->comment('记住 token');
            $table->unsignedTinyInteger('status')->nullable()->default(10)->comment('用户状态：启用-10 禁用-20');
            $table->timestamps(0);
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_users` COMMENT = '后台用户表';");

        Schema::create('admin_roles', function (Blueprint $table) {
            $table->id();
            $table->string('name', 128)->comment('角色名称');
            $table->string('slug', 64)->comment('角色标识符');
            $table->unsignedMediumInteger('sort')->nullable()->default(99)->comment('排序');
            $table->string('memo', 191)->nullable()->default('')->comment('备注');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_roles` COMMENT = '后台角色表';");

        Schema::create('admin_permissions', function (Blueprint $table) {
            $table->id();
            $table->string('name', 64)->comment('权限名称');
            $table->string('slug', 64)->comment('权限标识符');
            $table->unsignedMediumInteger('sort')->nullable()->default(99)->comment('排序');
            $table->string('memo', 191)->nullable()->default('')->comment('备注');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_permissions` COMMENT = '后台权限表';");

        Schema::create('admin_menus', function (Blueprint $table) {
            $table->id();
            $table->unsignedMediumInteger('parent_id')->nullable()->default(0)->comment('父级菜单栏');
            $table->string('title', 64)->comment('设置该路由在侧边栏和面包屑中展示的名字');
            $table->string('path', 64)->comment('菜单栏名称');
            $table->string('icon', 64)->nullable()->default('')->comment('菜单栏图标');
            $table->unsignedMediumInteger('sort')->nullable()->default(99)->comment('排序');
            $table->unsignedMediumInteger('hidden')->nullable()->default(10)->comment('当设置 true 的时候该路由不会在侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_menus` COMMENT = '后台菜单栏';");

        Schema::create('admin_role_user', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('role_id')->comment('角色ID');
            $table->unsignedInteger('user_id')->comment('用户ID');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_role_user` COMMENT = '后台角色用户关联表';");

        Schema::create('admin_role_menu', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('role_id')->comment('角色ID');
            $table->unsignedInteger('menu_id')->comment('菜单栏ID');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_role_menu` COMMENT = '后台角色菜单栏关联表';");

        Schema::create('admin_role_permission', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('role_id')->comment('角色ID');
            $table->unsignedInteger('permission_id')->comment('用户ID');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `laravel_api`.`admin_role_permission` COMMENT = '后台角色权限关联表';");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admin_users');
        Schema::dropIfExists('admin_roles');
        Schema::dropIfExists('admin_permissions');
        Schema::dropIfExists('admin_menus');

        // 中间表
        Schema::dropIfExists('admin_role_users');
        Schema::dropIfExists('admin_role_permissions');
    }
}
